#include "../process.h"
#include "../config.h"
#include "../util.h"

int main(int argc, const char* argv[])
{
	string fn(argv[1]);
	fstream input(fn, fstream::in);
	fstream stop_query_file(fn + "_stop.txt", fstream::out);
	fstream output(fn + "_out.txt", fstream::out);
	set<string> stop_query;
	int threshold = atoi(argv[2]);
	while(!input.eof())
	{
		string line;
		getline(input, line);
		int del = line.find(":");
		string query = line.substr(0, del);
		string elem_s = line.substr(del + 1, line.length() - del - 1);
		vector<string> elem = util::tokenize_h(elem_s, " | ");
		if(elem.size() > threshold)
			stop_query.insert(query);
	}
	for(set<string>::iterator itr = stop_query.begin(); itr != stop_query.end(); ++ itr)
		stop_query_file << *itr << endl;
	stop_query_file.close();
	input.close();
	fstream input_1(fn, fstream::in);
	while(!input_1.eof())
	{
		string line;
		getline(input_1, line);
		int del = line.find(":");
		string query = line.substr(0, del);
		if(stop_query.find(query) != stop_query.end())continue;
		string elem_s = line.substr(del + 1, line.length() - del - 1);
		vector<string> elem = util::tokenize_h(elem_s, " | ");	
		vector<string> filtered_elem;
		for(int i = 0; i < elem.size(); ++ i)
			if(stop_query.find(elem[i]) == stop_query.end())
				filtered_elem.push_back(elem[i]);
		if(filtered_elem.size() > 0)
		{
			output << query << ":";
			for(int i = 0; i < filtered_elem.size(); ++ i)
				output << filtered_elem[i] << " | ";
			output << endl;
		}
	}
	input_1.close();
	output.close();
	return 0;
}